package com.yunlongn.common.json.jackson.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.yunlongn.common.json.jackson.factory.AbstractJsonDeserializer;
import com.yunlongn.common.json.util.StringUtils;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * double保留两位小时
 * @author Yun
 * @since 20210902
 */
public class DoubleDeserializer extends AbstractJsonDeserializer<Double> {

    @Override
    public Double deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
            throws IOException {
        String value = jsonParser.getText();
        if (StringUtils.isNullOrEmpty(value) || "null".equalsIgnoreCase(value)) {
            return null;
        }
        return new BigDecimal(value).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
    }

}
